#################################
# Online Appendix F
# Experiment 3: Independents
#################################

###Log
my_log <- file("appendix_OF_log.txt")

sink(my_log, append=T, type="output")
sink(my_log, append=T, type="message")

cat(readChar(rstudioapi::getSourceEditorContext()$path, # Writing currently opened R script to file
             file.info(rstudioapi::getSourceEditorContext()$path)$size))

start_time <- Sys.time()
start_time

###Packages

library(rio)
library(tidyverse)
library(ggstance)
library(margins)
library(ggpubr)


############
#Data
############

exp3 <- import("./Data/exp3_cleaned.csv") %>%
  filter(pid_3 == "Independent")

############
#Coding Treatment Indicators for Independents
############
exp3a <- exp3 %>%
  mutate(cues_comb = case_when(
    cue_treat %in% c("dem_conchange_nocue", "dem_libchange_nocue",
                     "rep_conchange_nocue", "rep_libchange_nocue") ~ 1, 
    #Dem Supporot
    cue_treat %in% c("dem_conchange_cue", "dem_libchange_cue") ~ 2,
    #Dem Support + Insinuation
    cue_treat %in% c("dem_conchange_cue_companies", "dem_conchange_cue_interestgroups",
                     "dem_conchange_cue_unions", "dem_libchange_cue_companies",
                     "dem_libchange_cue_interestgroups", "dem_libchange_cue_unions")  ~ 3, 
    #Rep Support
    cue_treat %in% c("rep_conchange_cue", "rep_libchange_cue") ~ 4, 
    #Rep Support Insinuation
    cue_treat %in% c("rep_conchange_cue_companies", "rep_conchange_cue_interestgroups",
                     "rep_conchange_cue_unions", "rep_libchange_cue_companies", 
                     "rep_libchange_cue_interestgroups", "rep_libchange_cue_unions") ~ 5), 
    cues_all = case_when(
      cue_treat %in% c("dem_conchange_nocue", "dem_libchange_nocue",
                       "rep_conchange_nocue", "rep_libchange_nocue") ~ 1, 
      #Dem Support
      cue_treat %in% c("dem_conchange_cue", "dem_libchange_cue") ~ 2,
      #Dem: IGs
      cue_treat %in% c( "dem_libchange_cue_interestgroups", "dem_conchange_cue_interestgroups") ~ 3,
      #Dem: Companies
      cue_treat %in% c("dem_conchange_cue_companies", "dem_libchange_cue_companies") ~ 4, 
      #Dem: Unions
      cue_treat %in% c( "dem_conchange_cue_unions", "dem_libchange_cue_unions") ~ 5, 
      #Rep Support
      cue_treat %in% c("rep_conchange_cue", "rep_libchange_cue") ~ 6, 
      #Rep: IGs
      cue_treat %in% c("rep_conchange_cue_interestgroups", "rep_libchange_cue_interestgroups") ~ 7,
      #Rep: Companies 
      cue_treat %in% c("rep_conchange_cue_companies",  "rep_libchange_cue_companies") ~ 8, 
      #Rep: Unions
      cue_treat %in% c( "rep_conchange_cue_unions", "rep_libchange_cue_unions") ~9)) %>%
  mutate(cues_comb = factor(cues_comb, 
                            levels=c(1,2,3,4,5), 
                            labels=c("No Cue", "Dems Support", "Dems + Insinuation", 
                                     "Reps Support", "Reps + Insinuation")), 
         cues_all = factor(cues_all, 
                           levels=c(1,2,3,4,5,6,7,8,9), 
                           labels=c("No Cue", "Dems Support", 
                                    "Dems + IGs", "Dems + Companies", 
                                    "Dems + Unions", 
                                    "Reps Support" , "Reps + IGs", 
                                    "Reps + Companies", "Reps + Unions")))

############################
#Figure OF1
############################
exp3a <- exp3a %>%
  mutate(change = factor(change, 
                         levels=c("Conservative Change", "Liberal Change")))

exp3a$support01 <- scales::rescale(exp3a$tax_support)

m1 <- lm(support01 ~ cues_comb + change, data=exp3a)
m2 <- lm(support01 ~ cues_comb*change, data = exp3a)

##margins
m1_margs <- summary(margins(m1, 
                            variables="cues_comb")) %>%
  rename(cond = factor) %>%
  mutate(cond = socviz::prefix_strip( cond, "cues_comb"), 
         cond = factor(cond, 
                       levels=c("Reps + Insinuation",
                                "Reps Support", 
                                "Dems + Insinuation", 
                                "Dems Support"))) %>%
  ggplot(aes(x=AME, y=cond)) + 
  geom_pointrangeh(aes(xmin=lower, xmax=upper)) + 
  labs(title = "Model 1 Results",
       x = "AME", y = NULL) + 
  theme_light() + 
  geom_vline(xintercept=0, color="red", linetype="dashed")


m2_lib <- summary(margins(m2, 
                          variables="cues_comb", 
                          at = list(change = c("Liberal Change")))) %>%
  rename(cond = factor) %>%
  mutate(cond = socviz::prefix_strip( cond, "cues_comb"), 
         cond = factor(cond, 
                       levels=c("Reps + Insinuation",
                                "Reps Support", 
                                "Dems + Insinuation", 
                                "Dems Support"))) %>%
  ggplot(aes(x=AME, y=cond)) + 
  geom_pointrangeh(aes(xmin=lower, xmax=upper)) + 
  labs(title = "Model 2 Results: Liberal Change",
       x = "AME", y = NULL) + 
  theme_light() + 
  geom_vline(xintercept=0, color="red", linetype="dashed")


m2_cons <- summary(margins(m2, 
                           variables="cues_comb", 
                           at = list(change = c("Conservative Change")))) %>%
  rename(cond = factor) %>%
  mutate(cond = socviz::prefix_strip( cond, "cues_comb"), 
         cond = factor(cond, 
                       levels=c("Reps + Insinuation",
                                "Reps Support", 
                                "Dems + Insinuation", 
                                "Dems Support"))) %>%
  ggplot(aes(x=AME, y=cond)) + 
  geom_pointrangeh(aes(xmin=lower, xmax=upper)) + 
  labs(title = "Model 2 Results: Conservative Change",
       x = "AME", y = NULL) + 
  theme_light() + 
  geom_vline(xintercept=0, color="red", linetype="dashed")


ggarrange(m1_margs, m2_lib, m2_cons)

ggsave("./Online Appendix F/figure_of1.png", 
       height=7, width=14)



############################
#Figure OF2
############################

m3 <- lm(support01 ~ cues_all + change, data=exp3a)
m4 <- lm(support01 ~ cues_all*change, data = exp3a)

##margins
m3_margs <- summary(margins(m3, 
                            variables="cues_all")) %>%
  rename(cond = factor) %>%
  mutate(cond = socviz::prefix_strip( cond, "cues_all"), 
         cond = factor(cond, 
                       levels=c("Reps + Unions", 
                                "Reps + Companies", 
                                "Reps + IGs", 
                                "Reps Support", 
                                "Dems + Unions", 
                                "Dems + Companies", 
                                "Dems + IGs", 
                                "Dems Support"))) %>%
  ggplot(aes(x=AME, y=cond)) + 
  geom_pointrangeh(aes(xmin=lower, xmax=upper)) + 
  labs(title = "Model 1 Results",
       x = "AME", y = NULL) + 
  theme_light() + 
  geom_vline(xintercept=0, color="red", linetype="dashed")


m4_lib <- summary(margins(m4, 
                          variables="cues_all", 
                          at = list(change = c("Liberal Change")))) %>%
  rename(cond = factor) %>%
  mutate(cond = socviz::prefix_strip( cond, "cues_all"), 
         cond = factor(cond, 
                       levels=c("Reps + Unions", 
                                "Reps + Companies", 
                                "Reps + IGs", 
                                "Reps Support", 
                                "Dems + Unions", 
                                "Dems + Companies", 
                                "Dems + IGs", 
                                "Dems Support")))  %>%
  ggplot(aes(x=AME, y=cond)) + 
  geom_pointrangeh(aes(xmin=lower, xmax=upper)) + 
  labs(title = "Model 2 Results: Liberal Change",
       x = "AME", y = NULL) + 
  theme_light() + 
  geom_vline(xintercept=0, color="red", linetype="dashed")


m4_cons <- summary(margins(m4, 
                           variables="cues_all", 
                           at = list(change = c("Conservative Change")))) %>%
  rename(cond = factor) %>%
  mutate(cond = socviz::prefix_strip( cond, "cues_all"), 
         cond = factor(cond, 
                       levels=c("Reps + Unions", 
                                "Reps + Companies", 
                                "Reps + IGs", 
                                "Reps Support", 
                                "Dems + Unions", 
                                "Dems + Companies", 
                                "Dems + IGs", 
                                "Dems Support")))  %>%
  ggplot(aes(x=AME, y=cond)) + 
  geom_pointrangeh(aes(xmin=lower, xmax=upper)) + 
  labs(title = "Model 2 Results: Conservative Change",
       x = "AME", y = NULL) + 
  theme_light() + 
  geom_vline(xintercept=0, color="red", linetype="dashed")

ggarrange(m3_margs, m4_lib, m4_cons)

ggsave("./Online Appendix F/figure_of2.png", 
       height=7, width=14)


end_time <- Sys.time()
end_time

closeAllConnections()

